<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/17 0017
 * Time: 上午 9:58
 */

namespace App\Http\Classes;
use Illuminate\Support\Facades\DB;

class UserHard
{
    private $tableName = 'user_hard';
    private $tableDB = null;
    private $error = 0;
    private $message = [
        0 => '无数据',
        1 => '写入数据库失败',
        2 => '将要删除的数据不存在'
    ];

    public function __construct( $id = null )
    {
        $this->tableDB = DB::table( $this->tableName );

        if ( $id !== null )
        {
            $this->tableDB = $this->tableDB->where('id', '=', $id);
        }
    }

    public function getUserHardWords( $userId )
    {
        $result = $this->tableDB->where('type', '=', 1)->where('user_id', '=', $userId)
            ->join('words', $this->tableName . '.word_id', '=', 'words.id')
            ->join('lessons', 'lessons.id', '=', 'words.lesson_id')
            ->join('books', 'books.id', '=', 'lessons.book_id')
            ->select(
                'user_hard.id as hard_id',
                'words.word',
                'words.word_meaning',
//                     'words.sentence_audio',
//                     'words.word_audio',
                'lessons.number as lesson',
                'books.book as book')->get();

        if ( $result )
        {
//            for ( $i = 0; $i < count($result); $i++ )
//            {
//                $result[$i]->sentence_audio = HOST . $result[$i]->sentence_audio;
//                $result[$i]->word_audio = HOST . $result[$i]->word_audio;
//            }
            return $result;
        }
        else
        {
            $this->error = 0;
            return null;
        }
    }

    public function getUserHardSentences( $userId )
    {
        $result = $this->tableDB->where('type', '=', 2)->where('user_id', '=', $userId)
            ->join('sentences', $this->tableName . '.sentence_id', '=', 'sentences.id')
            ->join('lessons', 'lessons.id', '=', 'sentences.lesson_id')
            ->join('books', 'books.id', '=', 'lessons.book_id')
            ->select(
                'user_hard.id as hard_id',
                'sentences.sentence',
                'sentences.sentence_meaning',
                'lessons.number as lesson',
                'books.book as book')->get();

        if ( $result )
        {
//            for ( $i = 0; $i < count($result); $i++ )
//            {
//                $result[$i]->sentence_audio = HOST . $result[$i]->sentence_audio;
//                $result[$i]->word_audio = HOST . $result[$i]->word_audio;
//            }

            return $result;
        }
        else
        {
            $this->error = 0;
            return null;
        }
    }

    public function addHardWord( array $data )
    {
        $result = $this->tableDB->insert($data);

        if ( $result )
        {
            return true;
        }
        else
        {
            $this->error = 1;
            return false;
        }
    }

    public function addHardSentence( array $data )
    {
        $result = $this->tableDB->insert($data);

        if ( $result )
        {
            return true;
        }
        else
        {
            $this->error = 1;
            return false;
        }
    }

    public function delWord($userId)
    {
        $result = $this->tableDB->where('user_id', '=', $userId)->where('type', '=', 1)->delete();

        if ($result)
        {
            return true;
        }
        else
        {
            $this->error = 2;
            return false;
        }
    }

    public function delSentence($userId)
    {
        $result = $this->tableDB->where('user_id', '=', $userId)->where('type', '=', 2)->delete();

        if ($result)
        {
            return true;
        }
        else
        {
            $this->error = 2;
            return false;
        }
    }

    public function delWords(array $hards, $userId)
    {
        $result = $this->tableDB->whereIn('id', $hards)->where('user_id', '=', $userId)->where('type', '=', 1)->delete();

        if ($result > 0)
        {
            return true;
        }
        else
        {
            $this->error = 2;
            return false;
        }
    }

    public function delSentences(array $hards, $userId)
    {
        $result = $this->tableDB->whereIn('id', $hards)->where('user_id', '=', $userId)->where('type', '=', 2)->delete();

        if ($result > 0)
        {
            return true;
        }
        else
        {
            $this->error = 2;
            return false;
        }
    }

    public function getMessage()
    {
        return $this->message[ $this->error ];
    }
}